package com.csthink.auditlog.enums;

import com.csthink.common.domain.KV;
import com.csthink.common.infrastructure.util.I18nUtil;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Getter;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author <a href="mailto:security.2009@live.cn">Mars</a>
 * @since 2023-09-12
 */
@JsonFormat(shape = JsonFormat.Shape.OBJECT)
public enum ActionEnum {

    UNKNOWN("Unknown", ""),
    ADD("Add", "alg.action.add"),
    DELETE("Delete", "alg.action.delete"),
    SYNC("Sync", "alg.action.sync"),
    UPDATE("Update", "alg.action.update"),
    OPERATE("Operate", "alg.action.operate"),
    MOVE("Move", "alg.action.move"),
    READ("Read", "alg.action.read"),
    ASSIGN("Assign", "alg.action.assign"),
    DOWNLOAD("Download", "alg.action.download"),
    RECOVER("Recover", "alg.action.recover"),
    IMPORT("Import", "alg.action.import"),
    EXPORT("Export", "alg.action.export"),
    UNMASK("Unmask", "alg.action.unmask"),
    SUBSCRIBE("Subscribe", "alg.action.subscribe"),
    AGREE("Agree", "alg.action.agree"),
    GENERATE("Generate", "alg.action.generate"),
    ENABLE("Enable", "alg.action.enable"),
    DISABLE("Disable", "alg.action.disable"),
    PUBLISH("Publish", "alg.action.publish"),
    UN_PUBLISH("UnPublish", "alg.action.unpublished"),
    OPT_OUT("Opt-Out", "alg.action.optOut"),
    SYNC_DATA("SyncData", "alg.action.syncData"),
    INITIATE("Initiate", "alg.action.initiate"),
    SUBMIT("Submit", "alg.action.submit");

    private final String auditLogName;

    @Getter
    private final String i18nKey;

    ActionEnum() {
        this.auditLogName = "";
        this.i18nKey = "";
    }

    ActionEnum(String auditLogName, String i18nKey) {
        this.auditLogName = auditLogName;
        this.i18nKey = i18nKey;
    }

    public String toAuditLog() {
        return this.auditLogName;
    }

    public String getName() {
        return null == auditLogName ? this.name() : auditLogName;
    }


    public static ActionEnum[] getValues() {
        return Stream.of(values())
                .filter(c -> (!StringUtils.equalsIgnoreCase(c.auditLogName, ActionEnum.UNKNOWN.name())
                        && StringUtils.isNotBlank(c.auditLogName)))
                .map(v -> I18nUtil.getMessage(v.getI18nKey()))
                .toArray(ActionEnum[]::new);
    }

    public static List<KV> getI18nValues() {
        return Stream.of(values())
                .filter(c -> (!StringUtils.equalsIgnoreCase(c.auditLogName, ActionEnum.UNKNOWN.name())
                        && StringUtils.isNotBlank(c.auditLogName)))
                .map(c -> KV.builder().name(I18nUtil.getMessage(c.getI18nKey())).id(c.getName()).build())
                .collect(Collectors.toList());
    }

    public static ActionEnum getAction(String auditLogName) {
        return Arrays.stream(values()).filter(e -> e.getName().equalsIgnoreCase(auditLogName)).findAny().orElse(null);
    }
}
